package com.cms.biz.queryAnalyse.query;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Repository;

import com.common.util.EMPSQLUtil;
import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.sql.JConnection;
import com.efounder.sql.JStatement;
import com.enums.EMPQueryParamEnum;
import com.mrp.persistence.queryAnalyse.pageQuery.bean.EMPPageQuery;
import com.pub.util.EMPLimitConvertUtil;
import com.pub.util.StringFunction;


/**
 * <p>Title: 项目查询分析</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
@Repository
public class EMPCCNRLCCCTZDQuery {

	@Autowired
	private JdbcTemplate                  jdbcTemplate;
	
	protected     String                   msTableName = "";
	
	protected     String                  msTableName1 = "";
	
	public JResponseObject QueryObject(JParamObject PO){
		JConnection         conn = null;
		JStatement          stmt = null;
		EFDataSet   queryDataset = null;
		EFRowSet     queryRowset = EFRowSet.getInstance();
		JResponseObject       RO = null;
		try {
			conn = JConnection.getInstance(jdbcTemplate.getDataSource().getConnection());
			conn.setAutoCommit(true);
			stmt = conn.createStatement();
			
			// 获取查询数据
			queryDataset = queryDataObject(stmt, PO);
			queryRowset.setExtProperty(EMPQueryParamEnum.PAGEQUERY, PO.getValue(EMPQueryParamEnum.PAGEQUERY, null));
			queryRowset.setDataSet(EMPQueryParamEnum.QUERYRESULT, queryDataset);
			// 封装返回数据
		   	RO = new JResponseObject(queryRowset, 0);
		} catch(Exception ce) {
			ce.printStackTrace();
			RO = new JResponseObject(null, -1, ce);
		} finally {
			try {
				stmt.executeUpdate("drop table "+msTableName);
				stmt.executeUpdate("drop table "+msTableName1);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			EMPSQLUtil.closeAllResources(null, stmt, conn);
		}
		return RO;
	}
	
	/**
	 * 组织数据的过程
	 * @throws Exception
	 * @return TreeTableDataManager
	 */
	private EFDataSet queryDataObject(Statement stmt, JParamObject po) throws Exception{
		EFDataSet   queryDataset = null;
		//创建临时表
	    createTempTable(stmt);
	    
	    //取出原始数据
	    getOriginData(stmt, po);
	    
	    //创建临时表
	    queryDataset = convertDataSet(stmt, po);
		return queryDataset;
	}
	
	/**
	 * 创建临时表
	 * @throws Exception
	 */
	private void createTempTable(Statement stmt) throws Exception{
	    msTableName = "SCTZ"+StringFunction.getTempStr(6);
	    String        strSql = "";
	    
	    String tempTableBody = " F_ID          varchar(1)           default ''   null,"
	         				 + " F_KJQJ        varchar(255)         default ''   null,"
	         				 + " F_DJBH        varchar(255)         default ''   null,"
	         				 + " F_GUID        varchar(255)         default ''   null,"
	         				 + " F_ZDR         varchar(255)         default ''   null,"
	         				 + " F_ZDRMC       varchar(255)         default ''   null,"
	         				 + " F_ZDRQ        varchar(255)         default ''   null,"
	         				 + " F_HTBH        varchar(255)         default ''   null,"
	         				 + " F_HTMC        varchar(255)         default ''   null,"
	         				 + " F_XMBH        varchar(255)	        default ''   null,"
	         				 + " F_XMMC        varchar(255)   	    default ''   null,"
	         				 + " F_JSQD        varchar(255)   	    default ''   null,"
	         				 + " F_SFCQ        varchar(255)   	    default ''   null,"
	         				 + " F_WGRQ        varchar(255)   	    default ''   null,"
	         				 + " F_JSFZR       varchar(255)   	    default ''   null,"
	         				 + " F_JSFZRMC     varchar(255)   	    default ''   null,"
	         				 + " SCSQ_KJQJ     varchar(255)   	    default ''   null,"
	         				 + " SCSQ_DJBH     varchar(255)   	    default ''   null,"
	         				 + " SCSQ_GUID     varchar(255)   	    default ''   null,"	 
	         				 + " F_NOTE        varchar(255)   	    default ''   null,"
	         				 + " F_SYZT        varchar(255)         default ''   null";
	    strSql = "create table " + msTableName + "(" + tempTableBody + ")";
	    stmt.execute(strSql);
	    
	    msTableName1 = "SCSYSL"+StringFunction.getTempStr(6);

	    tempTableBody = " F_ID          varchar(1)           default ''   null,"
	         		  + " F_KJQJ        varchar(255)         default ''   null,"
	         		  + " F_DJBH        varchar(255)         default ''   null,"
	         		  + " F_GUID        varchar(255)         default ''   null,"	         		
	         		  + " F_SCSL        decimal(30,2)        default 0    null,"
	         		  + " F_CCSL        decimal(30,2)   	 default 0    null,"
	         		  + " F_SYSL        decimal(30,2)   	 default 0    null,"
	         		  + " F_SYZT        varchar(255)         default ''   null";
	    strSql = "create table " + msTableName1 + "(" + tempTableBody + ")";
	    stmt.execute(strSql);
	  }
	  
	/**
	 * 逐级汇总数据
	 * @throws Exception
	 */
	public void getOriginData(Statement stmt, JParamObject po) throws Exception {
		String              strSql = "";
		String              F_QSQJ = po.GetValueByParamName("F_QSQJ", "");
		String              F_JZQJ = po.GetValueByParamName("F_JZQJ", "");
		String              F_XMMC = po.GetValueByParamName("F_XMMC", "");

		//插入入库数量
		strSql = " insert into " + msTableName + " (F_ID, F_KJQJ, F_DJBH, F_GUID, F_ZDR, F_ZDRMC, F_ZDRQ, F_HTBH, F_HTMC, " 
		       + " F_XMBH, F_XMMC, F_NOTE, F_JSQD, F_SFCQ, F_WGRQ, F_JSFZR, F_JSFZRMC, SCSQ_KJQJ, SCSQ_DJBH, SCSQ_GUID, F_SYZT)"
		       + " select '0', SCTZXX.F_KJQJ, SCTZXX.F_DJBH, SCTZXX.F_GUID, SCTZXX.F_ZDR,F_ZDR.USER_NAME, "
		       + " SCTZXX.F_ZDRQ, SCTZXX.F_HTBH, F_HTBH.F_HTMC, SCTZXX.F_XMBH,F_XMBH.F_XMMC, SCTZXX.F_NOTE, SCTZXX.F_JSQD, "
		       + " SCTZXX.F_SFCQ, SCTZXX.F_WGRQ, SCTZXX.F_JSFZR, F_JSFZR.F_ZGMC, SCTZXX.SCSQ_KJQJ, SCTZXX.SCSQ_DJBH, SCTZXX.SCSQ_GUID, '0'"
		       + " from SCTZXX"
		       + " LEFT JOIN HTXXZD F_HTBH ON F_HTBH.F_HTBH = SCTZXX.F_HTBH"
		       + " LEFT JOIN HYZGZD F_JSFZR ON F_JSFZR.F_ZGBH = SCTZXX.F_JSFZR "
		       + " LEFT JOIN HTXMZD F_XMBH ON F_XMBH.F_XMBH = SCTZXX.F_XMBH and F_XMBH.F_XMMC like '%" + F_XMMC + "%'"
		       + " LEFT JOIN BSUSER F_ZDR ON F_ZDR.USER_ID = SCTZXX.F_ZDR "
		       + " where SCTZXX.F_KJQJ <= '" + F_JZQJ + "' and SCTZXX.F_KJQJ >= '" + F_QSQJ + "'"		       
		       + " order by SCTZXX.F_DJBH";
		stmt.execute(strSql);
		
		//插入入库数量
		strSql = " insert into " + msTableName1 + " (F_ID, F_KJQJ, F_DJBH, F_GUID, F_SYZT)"
		       + " select '0', F_KJQJ, F_DJBH, F_GUID, '1'"
		       + " from SCSYSL_V"
		       + " where F_KJQJ <= '" + F_JZQJ + "' and F_KJQJ >= '" + F_QSQJ + "'"	
		       + " and (F_SCSL - F_CCSL) > 0"
		       + " group by F_KJQJ, F_DJBH, F_GUID"
		       + " order by F_DJBH";
		stmt.execute(strSql);
	}
	
	public EFDataSet convertDataSet(Statement stmt, JParamObject po) throws Exception {
		EFDataSet queryDataset = null;
		EFDataSet   newDataset = EFDataSet.getInstance();
		ResultSet           rs = null;
		String          strSql = ""; 
		EMPPageQuery pageQuery = (EMPPageQuery) po.getValue(EMPQueryParamEnum.PAGEQUERY, "");

		//删除项目名称为空的记录
		strSql = " delete from " + msTableName + " where F_XMMC is null";
		stmt.execute(strSql);
		
		//更新所属项目名称
		strSql = " update " + msTableName + " TEMP"
	           + " INNER JOIN " + msTableName1 + " TEMP1 ON TEMP.F_KJQJ = TEMP1.F_KJQJ and TEMP.F_DJBH = TEMP1.F_DJBH and TEMP.F_GUID = TEMP1.F_GUID set TEMP.F_SYZT = TEMP1.F_SYZT" ;
		stmt.execute(strSql);

		strSql = " select * from " + msTableName + " order by F_KJQJ, F_DJBH";
		rs = stmt.executeQuery(strSql);
		queryDataset = DataSetUtils.resultSet2DataSet(rs, queryDataset);

		if(pageQuery == null) return queryDataset;
		
		pageQuery.setTotalCount(queryDataset.getRowCount());
		newDataset = EMPLimitConvertUtil.getDataSetLimit(queryDataset, pageQuery.getPageNum(), pageQuery.getNumPerPage());
		return newDataset;
	}
}
